<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2022/12/6
 * Time: 19:01
 */

namespace app\controller\sys;
use app\model\app\AppLoanOrder;
use app\model\app\AppPay AS model;
use app\model\app\AppPay;
use app\model\app\AppPayOrder AS order;
use app\model\app\AppPayOrder;
use app\model\sys\SysAutoPayConfig AS config;

use think\Exception;

class Pay extends Base
{
    public function list(){
        $where = $this->where;
        $post = input('post.');
        !empty($this->start_time) && $where[] = ['create_time','>=',$this->start_time];
        !empty($this->end_time) && $where[] = ['create_time','<=',$this->end_time];
        $list = model::where($where)
            ->order('create_time DESC')
            ->paginate([
                'page' => $this->page,
                'list_rows' => $this->size
            ]);
        return $this->msg($list);
    }

    public function off(){
        $id = input('id');
        $find = model::where('id',$id)->find();
        if (empty($find->id))
            return $this->fail('数据不存在');

    }

    public function info(){
        $id = input('id');
        $user = model::where('id',$id)->find();
        return $this->msg($user);
    }

    public function save(){
        $post = input('post.');
        if (empty($post['id'])) {
            model::create($post);
        }else{
            $find = model::where('id',$post['id'])->find();
            if (empty($find->id))
                return $this->fail('数据不存在');
            $find->save($post);
        }
        return $this->msg();
    }

    public function del(){
        $user_id = input('id');
        $find = model::where('id',$user_id)->find();
        if (empty($find->id))
            return $this->fail('数据不存在');
        $find->delete();
        return $this->msg();
    }

    public function order_list(){
        $where = $this->where;
        $post = input('post.');
        !empty($post['user_id']) && $where[] = ['user_id','=',$post['user_id']];
        !empty($post['order_code']) && $where[] = ['order_code','LIKE','%'.$post['order_code'].'%'];
        !empty($post['phone']) && $where[] = ['phone','LIKE','%'.$post['phone'].'%'];
        !empty($post['kefu_name']) && $where[] = ['kefu_name','LIKE','%'.$post['kefu_name'].'%'];
        !empty($post['status']) && $where[] = ['status','=',$post['status']];
        !empty($this->start_time) && $where[] = ['create_time','>=',$this->start_time];
        !empty($this->end_time) && $where[] = ['create_time','<=',$this->end_time];
        $list = order::where($where)
            ->order('create_time DESC')
            ->paginate([
                'page' => $this->page,
                'list_rows' => $this->size
            ]);
        return $this->msg($list);
    }

    public function order_info(){
        $id = input('id');
        $user = order::where('id',$id)->find();
        return $this->msg($user);
    }

    public function order_save(){
        $post = input('post.');
        if (empty($post['id'])) {
            order::create($post);
        }else{
            $find = order::where('id',$post['id'])->find();
            if (empty($find->id))
                return $this->fail('数据不存在');
            $find->save($post);
        }
        return $this->msg();
    }


    public function order_del(){
        $user_id = input('id');
        $find = order::where('id',$user_id)->find();
        if (empty($find->id))
            return $this->fail('数据不存在');
        $find->delete();
        return $this->msg();
    }

    public function amount_pay(){
        $find = AppPay::where('pay_ds',1)->find();
        $function = 'pay_pay'.$find->td;
        $result = $this->$function();
        !empty(json_decode($result)) && $result = json_decode($result);
        if ($find->td == 1){
            if ($result->respCode == 'SUCCESS'){
                return $this->msg($result);
            }else
                throw new Exception('fail:405');
        }elseif($find->td == 2){
            if (!empty($result->platformOrderCode)){
                return $this->msg($result);
            }else
                throw new Exception('fail:406');
        }
    }


    public function pay_pay1(){
        $id = input('id');
        $order = AppPayOrder::find($id);
        $find = AppPay::where('pay_ds',1)->find();
        $url = 'https://payment.qeapay.com/pay/web';
//        $key = 'a3a0b75ae9964c7ab08d8b8125cb6715';//测试
        $key = $find->pay_ds_key;
        $data = [
//            'mch_id' => '400002002',//测试
            'version' => '1.0',
            'mch_id' => $find->pay_no,
            'mch_order_no' => $order->order_code,
            'pay_type' => $find->pay_type,
            'order_date' => $order->create_time,
            'trade_amount' => floor($order->pay_amount),
            'goods_name' => 'thisisgoods'.$order->pay_amount,
            'notify_url' => $find->pay_ds_url
        ];
        ksort($data);
        foreach ($data AS $index => $value) $array[] = $index.'='.$value;
        $data['sign'] = MD5(implode('&',$array).'&key='.$key);
        $data['sign_type'] = 'MD5';
        $result = AppPay::http_post_res($data,$url);
        return $result;
    }

    public function pay_pay2(){
        $id = input('id');
//        $find = AppPay::where('pay_df',1)->find();
        $key = 'EzUuvFRjqKSwnkeQrm50';
        $url = 'https://hxpayment.top/payment/collection';
        $order = AppPayOrder::find($id)->toArray();
        $data = [
            'merchantLogin' => 'Blackcat',
            'orderCode' => $order['order_code'],//测试
            'amount' => bcadd($order['pay_amount'],0,2),
            'name' => $order['name'],
            'email' => $order['email'],
            'phone' => $order['phone']
        ];
        ksort($data);
        foreach ($data AS $index => $value)
            $array[] = $index.'='.$value;
        $data['sign'] = MD5(implode('&',$array).'&key='.$key);
        $result = AppPay::http_post_res($data,$url,true);
//        $result = doCurlPostRequest($url,$data);
        return ($result);
    }

    public function pay2_order_find_status(){
        $id = input('id');
        $order = AppPayOrder::where('id',$id)->find();
        $token = cache('pay2');
        if (empty($token)) {
            $url = 'https://hxpayment.top/api/authenticate';
            $data = ['username' => 'Blackcat', 'password' => '445566'];
            $token = AppPay::http_post_res($data, $url, true);
            $token = json_decode($token,true);
            cache('pay2',$token['id_token'],'86400');
        }
        $url = 'https://hxpayment.top/api/collection-orders?orderCode.equals='.$order->order_no;
        $find = AppPay::http_post_res([],$url,true,0,true);
        $find = json_decode($find,true);
        if (empty($find[0]))
            return $this->fail('订单不存在');
        $find = $find[0];
        if ($find['status'] == 'PENDING'){
            return $this->fail('订单在等待支付中');
        }elseif($find['status'] == 'SUCCESS'){
            $order->save([
                'update_time' => now(),
                'status' => 1,
                'is_credit' => $order->is_credit == 1 ? 2 : 0
            ]);
            if ($order->is_credit == 1){
                AppLoanOrder::where('id',$order->order_id)
                    ->inc('credit_amount',$find->pay_amount)
                    ->update(['update_time' => now(),'is_credit' => 2]);
            }
            return $this->msg();
        }else{
            return $this->fail('订单支付失败');
        }
    }


    public function config_list(){
        $where = [];
        $post = input('post.');
        !empty($this->start_time) && $where[] = ['expire_time','>=',$this->start_time];
        !empty($this->end_time) && $where[] = ['expire_time','<=',$this->end_time];
        $list = config::where($where)
            ->order('id DESC')
            ->paginate([
                'page' => $this->page,
                'list_rows' => $this->size
            ]);
        return $this->msg($list);
    }

    public function config_info(){
        $id = input('id');
        $user = config::where('id',$id)->find();
        return $this->msg($user);
    }

    public function config_save(){
        $post = input('post.');
        if (empty($post['id'])) {
            config::create($post);
        }else{
            $find = config::where('id',$post['id'])->find();
            if (empty($find->id))
                return $this->fail('数据不存在');
            $find->save($post);
        }
        return $this->msg();
    }

    public function config_del(){
        $user_id = input('id');
        $find = config::where('id',$user_id)->find();
        if (empty($find->uuid))
            return $this->fail('数据不存在');
        $find->delete();
        return $this->msg();
    }


}